<?php

include_once 'HelperDAO.class.php';

class CMSDAO {

    /**
     * @var $helperDAO HelperDAO
     */
    private $helperDAO;

    /**
     *
     */
    function __construct($config=false) {
        $this->helperDAO = new HelperDAO($config);
    }

    function getSections() {
        $sections = $this->helperDAO->select("SELECT * FROM section");
        foreach ($sections as $section) {
            $section->type = $this->helperDAO->select("SELECT * FROM sectionType where id='%s'", $section->sectionTypeId, true);
        }
        return $sections;
    }

    function getSubsections() {
        $subsections = $this->helperDAO->select("SELECT * FROM subsection");
        foreach ($subsections as $subsection) {
            $subsection->type = $this->helperDAO->select("SELECT * FROM subsectionType where id='%s'", $subsection->subsectionTypeId, true);
            $subsection->section = $this->helperDAO->select("SELECT * FROM section where id='%s'", $subsection->sectionId, true);
        }
        return $subsections;
    }

    function getContents() {
        $contents = $this->helperDAO->select("SELECT * FROM content");
        foreach ($contents as $content) {
            $content->subsection = $this->helperDAO->select("SELECT * FROM subsection where id='%s'", $content->subsectionId, true);
            $content->subsection->section = $this->helperDAO->select("SELECT * FROM section where id='%s'", $content->subsection->id, true);
            $content->subsection->section->type = $this->helperDAO->select("SELECT * FROM sectionType where id='%s'", $content->subsection->section->sectionTypeId, true);          
        }
        return $contents;
    }

    function getSectionTypes() {
        return $this->helperDAO->select("SELECT * FROM sectionType");
    }

    function getSubsectionTypes() {
        return $this->helperDAO->select("SELECT * FROM subsectionType");
    }

    function saveSection($sectionName, $sectionTypeId, $sectionStatus, $sectionOrder) {
        return $this->helperDAO->insert("INSERT INTO section(name,sectionTypeId,status_,order_) values ('%s','%s','%s','%s')", array($sectionName, $sectionTypeId, $sectionStatus, $sectionOrder));
    }

    function saveSubsection($subsectionName, $subsectionTypeId, $sectionId, $Status, $Order) {
        return $this->helperDAO->insert("INSERT INTO subsection(name,subsectionTypeId,sectionId,status_,order_) values ('%s','%s','%s','%s','%s')", array($subsectionName, $subsectionTypeId, $sectionId, $Status, $Order));
    }

    function saveContnet($Name, $subsectionId, $content, $Status, $Order) {
        return $this->helperDAO->insert("INSERT INTO content(name,subsectionId,content,status_,order_) values ('%s','%s','%s','%s','%s')", array($Name, $subsectionId, $content, $Status, $Order));
    }

    function editSection($sectionName, $sectionTypeId, $sectionStatus, $sectionOrder, $mainSubsectionId, $id) {
        if (!isset($mainSubsectionId) || $mainSubsectionId == 0) {
            $mainSubsectionId = "NULL";
        }
        return $this->helperDAO->update(
                "UPDATE section SET
                        name = '%s',
                        sectionTypeId = '%s',
                        status_ = '%s',
                        order_ = '%s',
                        mainSubSectionId=%s
                        WHERE id = '%s'", array($sectionName, $sectionTypeId, $sectionStatus, $sectionOrder, $mainSubsectionId, $id));
    }

    function editSubsection($sectionName, $sectionTypeId, $sectionStatus, $sectionOrder, $mainSubsectionId,$sectionId, $id) {
        if (!isset($mainSubsectionId) || $mainSubsectionId == 0) {
            $mainSubsectionId = "NULL";
        }
        return $this->helperDAO->update(
                "UPDATE subsection SET
                        name = '%s',
                        subsectionTypeId = '%s',
                        status_ = '%s',
                        order_ = '%s',
                        mainContentId=%s,
                        sectionId='%s'
                        WHERE id = '%s'", array($sectionName, $sectionTypeId, $sectionStatus, $sectionOrder, $mainSubsectionId, $sectionId,$id));
    }

    function editContent($Name,$subsectionId, $Status, $Order, $content, $id) {
        return $this->helperDAO->update(
                "UPDATE content SET
                        name = '%s',
                        subsectionId='%s',
                        status_ = '%s',
                        order_ = '%s',
                        content= '%s'
                        WHERE id = '%s'", array($Name, $subsectionId,$Status, $Order, $content, $id));
    }

    function getSection($id) {
        $section = $this->helperDAO->select("SELECT * FROM section WHERE id='%s'", array($id), true);
        $section->type = $this->helperDAO->select("SELECT * FROM sectionType where id='%s'", array($section->sectionTypeId));
        $section->subsections = $this->helperDAO->select("SELECT * FROM subsection where sectionId='%s'", array($section->id));
        return $section;
    }

    function getSubsection($id) {
        $section = $this->helperDAO->select("SELECT * FROM subsection WHERE id='%s'", array($id), true);
        $section->type = $this->helperDAO->select("SELECT * FROM subsectionType where id='%s'", array($section->sectionTypeId), true);
        $section->section = $this->helperDAO->select("SELECT * FROM section where id='%s'", array($section->sectionId), true);
        $section->content = $this->helperDAO->select("SELECT * FROM content where subsectionId='%s'", array($section->Id));
        return $section;
    }

    function getContent($id) {
        $content = $this->helperDAO->select("SELECT * FROM content where id ='%s'", $id, true);
        //$content->type = $this->helperDAO->select("SELECT * FROM subsectionType where id='%s'", $subsection->subsectionTypeId, true);
        $content->subsection = $this->helperDAO->select("SELECT * FROM subsection where id='%s'", $content->subsectionId, true);
        $content->subsection->section = $this->helperDAO->select("SELECT * FROM section where id='%s'", $content->subsection->sectionId, true);

        return $content;
    }

    function getSectionsSubsections($id){
        return $this->helperDAO->select("SELECT * FROM subsection where sectionId = '%s'", $id);
    }

}